<template>
    <div class="">
        <el-dialog
                :title="title"
                :visible.sync="dialogFormVisible"
                width="800px"
                :close-on-click-modal="false"
                @close="close"
        >
            <el-form ref="form" :model="form" label-width="180px">
                <el-form-item label="打印机名称" :required="true" prop="name">
                    <el-input  v-if="form.type != 4" v-model.trim="form.name" autocomplete="off" placeholder="请输入打印机名称"></el-input>
                    <el-select v-if="form.type == 4" v-model="form.name" placeholder="请选择">
                      <el-option
                          v-for="item in printer"
                          :key="item"
                          :label="item"
                          :value="item">
                      </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="打印机型号" :required="true">
                    <el-radio-group v-model="form.type">
                        <el-radio label="1">易联云K4</el-radio>
                        <el-radio label="2">飞鹅FP-58W</el-radio>
                        <el-radio label="3" v-show="form.support !='4'">飞鹅标签打印机FP-N20W</el-radio>
                        <el-radio label="4">本地打印机</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="出纸模式" :required="true" v-show="form.type=='3'">
                    <el-radio-group v-model="form.paper">
                        <el-radio label="1">正向出纸</el-radio>
                        <el-radio label="2">反向出纸</el-radio>
                    </el-radio-group>
                    <div class="yb_margin lh16">卷标纸默认尺寸为：40*30mm</div>
                </el-form-item>
                <div v-if="form.type=='1'">
                <el-form-item label="终端号" :required="true">
                    <el-input v-model="form.number" autocomplete="off" placeholder="请输入易联云终端号"></el-input>
                </el-form-item>
                <el-form-item label="设备秘钥" :required="true">
                    <el-input v-model="form.key" autocomplete="off" placeholder="请输入易联云设备秘钥"></el-input>
                </el-form-item>
                <el-form-item label="应用ID" :required="true">
                    <el-input v-model="form.userId" autocomplete="off" placeholder="请输入易联云应用ID"></el-input>
                </el-form-item>
                <el-form-item label="应用密钥" :required="true">
                    <el-input v-model="form.apiKey" autocomplete="off" placeholder="请输入易联云应用密钥"></el-input>
                </el-form-item>
                </div>
                <div v-if="form.type=='2'">
                    <el-form-item label="飞鹅打印机编号" :required="true">
                        <el-input v-model="form.dyjnumber" autocomplete="off" placeholder="请输入飞鹅打印机编号"></el-input>
                    </el-form-item>
                    <el-form-item label="飞鹅云后台注册账号" :required="true">
                        <el-input v-model="form.fenumber" autocomplete="off" placeholder="请输入飞鹅云后台注册账号"></el-input>
                    </el-form-item>
                    <el-form-item label="飞鹅UKEY" :required="true">
                        <el-input v-model="form.feukey" autocomplete="off" placeholder="请输入飞鹅UKEY"></el-input>
                    </el-form-item>
                </div>
                <div v-if="form.type=='3'">
                    <el-form-item label="飞蛾标签打印机编号" :required="true">
                        <el-input v-model="form.labeldyjnumber" autocomplete="off" placeholder="请输入飞蛾标签打印机编号"></el-input>
                    </el-form-item>
                    <el-form-item label="飞鹅标签云后台注册账号" :required="true">
                        <el-input v-model="form.labelfenumber" autocomplete="off" placeholder="请输入飞鹅标签云后台注册账号"></el-input>
                    </el-form-item>
                    <el-form-item label="飞鹅标签UKEY" :required="true">
                        <el-input v-model="form.labelfeukey" autocomplete="off" placeholder="请输入飞鹅标签UKEY"></el-input>
                    </el-form-item>
                </div>
                <el-form-item label="打印类型" :required="true">
                    <el-radio-group v-model="form.support">
                        <el-radio label="2">堂食</el-radio>
                        <el-radio label="3">快餐</el-radio>
                        <el-radio label="5">排队</el-radio>
<!--                        <el-radio label="4" v-show="form.type!='3'">收银</el-radio>-->
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="区域打印" v-if="form.support=='2'">
                  <el-select v-model="form.areaId" clearable multiple filterable placeholder="请选择">
                    <el-option
                        v-for="item in qyOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="打印模式" :required="true">
                    <el-radio-group v-model="form.pattern">
                        <el-radio label="1">商家联</el-radio>
                        <el-radio label="2" v-show="form.type !=='4'">顾客联</el-radio>
                        <el-radio label="3" v-show="form.support !='4' && form.type !=='4'">厨房联</el-radio>
                    </el-radio-group>
                </el-form-item>
                <div v-show="form.support !='4'">
                    <el-form-item label="打印方式" v-if="form.pattern=='1'" :required="true">
                        <el-radio-group v-model="form.printMode">
                            <el-radio label="1">整单打印</el-radio>
<!--                            <el-radio label="5" v-if="form.type!='3' && type=='1' || type=='2'">日结单打印</el-radio>-->
<!--                            <el-radio label="6" v-if="form.type!='3' && type=='1'">积分商城打印</el-radio>-->
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="打印方式" v-if="form.pattern=='2'" :required="true">
                        <el-radio-group v-model="form.printMode">
                            <el-radio label="1">整单打印</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="打印方式" v-if="form.pattern=='3'" :required="true">
                        <el-radio-group v-model="form.printMode">
                            <el-radio label="1">整单打印</el-radio>
                            <el-radio label="2">按商品分组打印</el-radio>
                            <el-radio label="3">一菜一单</el-radio>
                            <el-radio label="4">按标签打印</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="其他类型" v-if="form.support=='2' && form.pattern=='1'">
                        <el-checkbox-group v-model="form.printMode2">
                          <el-checkbox label="1">加菜</el-checkbox>
                          <el-checkbox label="2">退菜</el-checkbox>
<!--                          <el-checkbox label="3">订单退款</el-checkbox>-->
                        </el-checkbox-group>
                    </el-form-item>
                    <el-form-item label="其他类型" v-if="form.support=='2' && form.pattern=='3'">
                      <el-checkbox-group v-model="form.printMode2">
                        <el-checkbox label="1">加菜</el-checkbox>
                        <el-checkbox label="2">退菜</el-checkbox>
<!--                        <el-checkbox label="3">订单退款</el-checkbox>-->
                      </el-checkbox-group>
                    </el-form-item>
                </div>
                <div v-if="form.pattern=='3' && form.printMode=='2'">
                  <el-form-item label="分组打印方式" :required="true">
                    <el-radio-group v-model="form.printModeType">
                      <el-radio label="1">整单打印</el-radio>
                      <el-radio label="2">一菜一单</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
                <el-form-item label="商品分组" v-if="form.pattern=='3' && form.printMode=='2'" :required="true">
                    <el-select v-model="form.categoryIds" clearable multiple filterable allow-create placeholder="请选择">
                        <el-option  v-for="item in fzOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="打印标签" v-if="form.printMode=='4'" :required="true">
                    <el-select v-model="form.label" clearable multiple filterable allow-create placeholder="请选择">
                        <el-option  v-for="item in labelOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="打印联数" :required="true">
                    <el-input-number v-model="form.wmnum" :min="1" :max="10" label="描述文字"></el-input-number>
                    <span class="mar_l10">张</span>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="medium" @click="close">取 消</el-button>
                <el-button size="medium" type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {getSavePrint,postSavePrint} from "@/api/order";
    import { getLodop } from "@/assets/js/LodopFuncs";
    import {getArea} from "@/api/instore";

    export default {
        name: "labelSave",
        created() {
          console.log(1)
          var LODOP = getLodop()
          var COUNT = LODOP.GET_PRINTER_COUNT()
          console.log(COUNT)
          this.printer = []
          var i = 0
          while(i<COUNT){
            i++
            this.printer.push(LODOP.GET_PRINTER_NAME(i))
          }
          console.log(this.printer)
        },
        data() {
            return {
                form: {
                    name: '',
                    type: '1',
                    pattern: '1',
                    label: [],
                    categoryIds: [],
                    printMode2: [],
                    areaId: [],
                    printMode: '1',
                    printModeType: '1',
                    number: '',
                    key: '',
                    userId: '',
                    apiKey: '',
                    dyjnumber: '',
                    fenumber: '',
                    feukey: '',
                    labeldyjnumber: '',
                    labelfenumber: '',
                    labelfeukey: '',
                    wmnum: '',
                    paper: '1',
                    support: '2',
                },
                title: "",
                type: "",
                dialogFormVisible: false,
                labelOptions:[],
                fzOptions:[],
                qyOptions:[],
            }
        },
        components: {},
        methods: {
            async getArea() {
              const {data} = await getArea({storeId:''});
              this.qyOptions = data ? data : []
              // this.qyOptions.unshift({id:'',name:'全部'})
            },
            async init(){
                const { data } = await getSavePrint({item:9,storeId:this.form.storeId});
                this.fzOptions = data.goodsType;
                this.labelOptions = data.goodsLabel;
            },
            async showEdit(row) {
                this.init();
                this.getArea()
                if (!row) {
                    this.title = "添加打印机";
                } else {
                    this.title = "编辑打印机";
                    const { data } = await getSavePrint({id:row.id});
                    this.form2 = data.data
                    this.form = Object.assign(this.form2, data);
                }
                this.dialogFormVisible = true;
            },
            close() {
                this.$refs["form"].resetFields();
                this.form = this.$options.data().form;
                this.dialogFormVisible = false;
            },
            setType(type){
                this.type = type
            },
            setStoreId(storeId){
                this.form.storeId = storeId
            },
            save() {
                this.$refs["form"].validate(async (valid) => {
                    if (valid) {
                        // let data = this.form
                        // console.log(this.form,data)
                        this.$delete(this.form,'data')
                        const {msg} = await postSavePrint(this.form);
                        this.$baseMessage(msg, "success");
                        this.$emit("fetchData");
                        this.close();
                    } else {
                        return false;
                    }
                });
            },
        }
    };
</script>
<style lang="scss" scoped>
    .el-input-number{
        width: auto;
    }
</style>
